$(document).ready(function () {
  
  if (!getCookie("account")) {
    //清除
    console.log("执行删除");
    window.localStorage.clear();
    // if(window.localStorage.getItem("account") != null){
    //   window.localStorage.removeItem("account");
    // }
    // let orderItem = JSON.parse(window.localStorage.getItem("orderItem"));
    // if(orderItem != null){
    //   window.localStorage.removeItem("orderItem");
    // }
  }
  
  //取得cookie    
  function getCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';'); //把cookie分割成组
    console.log();
    for (var i = 0; i < ca.length; i++) {
      var c = ca[i]; //取得字符串    
      while (c.charAt(0) == ' ') { //判断一下字符串有没有前导空格    
        c = c.substring(1, c.length); //有的话，从第二位开始取    
      }
      if (c.indexOf(nameEQ) == 0) { //如果含有我们要的name    
        return unescape(c.substring(nameEQ.length, c.length)); //解码并截取我们要值    
      }
    };
    return false;
  }

 


  //获取账号
  let account = window.localStorage.getItem("account");
  


  //每个页面调用一次
  isLogin();

  //判断是否登陆 显示账户名和退出
  function isLogin() {


    if (account == null) {

      let login = $("<li></li>").append($("<a></a>").prop("class", "action_item").attr("href",
        "/woniumall2/html/login.html").text("登录").prop("id", "login_a"));

      let regist = $("<li></li>").append($("<a></a>").prop("class", "action_item").attr("href",
        "/woniumall2/html/login.html?regist").text("注册").prop("id", "regist_a"));

      $("#is_login").append(login).append(regist);
    } else {
      let account_li = $("<li></li>").append($("<a></a>").prop("class", "action_item").text("欢迎你:" + account));
      let quit = $("<li></li>").append($("<a></a>").prop("class", "action_item").attr("href", "#").text(
        "退出登陆"));
      cart_Nums();

      //退出点击事件
      quit.click(function () {
        if (account != null) {
          //发送请求 删除保存的的id
          $.ajax({
            type: "POST",
            url: "/woniumall2/user/cancellation",
            data: {
              'account': account
            },

            success: function (responseResult) {
              if (responseResult.status == "CANCELLTION_SUCCESS") {
                //删除session成功
                //关闭定时器
                clearInterval(timer);
                //调用退出按钮
                $('#quit_Popup').modal('show');

                quit_timer();
              } else if (responseResult.status == "CANCELLTION_FAIL") {
                //删除失败 后台session已经不在了
                window.location.href = "/woniumall2/index.html";
              }
            },
          });
        }
      });

      $("#is_login").append(account_li).append(quit);

      //调用计时器
     timer_login();
    }
  }


  //退出的定时器
  function quit_timer() {
    window.setInterval(function quit() {
      window.localStorage.removeItem("account");

      //删除cookie
      $.removeCookie('account', { path: '/' });
      
      window.location.href = "/woniumall2/index.html";
    }, 3000);
  }

  //现在跳转
  $("#now_time").click(function () {
    window.localStorage.removeItem("account");

    //删除cookie
    $.removeCookie('account', { path: '/' });

    window.location.href = "/woniumall2/index.html";
  })

  //去个人中心
  //my_center
  $("#my_center").click(function () {
    let url = "/woniumall2/html/personal.html";
  
    $.ajax({
      type: "POST",
      url: url,
      success: function (responseResult) {
        if (responseResult.code == "302") {
          alert("请登录后再操作");
          window.location.href = responseResult.data;
        } else {
          window.location.href = url;
        }
      },
    });
  });

  //去我的订单
  $("#my_orders").click(function () {
    let url ="/woniumall2/html/personal.html?personal";
    console.log(url);
    $.ajax({
      type: "POST",
      url: url,
      success: function (responseResult) {
        if (responseResult.code == "302") {
          alert("请登录后再操作");
          window.location.href = responseResult.data;
        } else {
          window.location.href = url;
        }
      }
    });
  });

  //去我的购物车
  $("#my_cart").click(function () {
    let url = "/woniumall2/cart.html";
    console.log(url);
    $.ajax({
      type: "POST",
      url: url,
      success: function (responseResult) {
        if (responseResult.code == "302") {
          alert("请登录后再操作");
          window.location.href = responseResult.data;
        } else {
          window.location.href = url;
        }
      },

    });
  });

  $("#my_cart_image").click(function () {
    let url = "/woniumall2/cart.html";
    console.log(url);
    $.ajax({
      type: "POST",
      url: url,
      success: function (responseResult) {
        if (responseResult.code == "302") {
          alert("请登录后再操作");
          window.location.href = responseResult.data;
        } else {
          window.location.href = url;
        }
      },
    });
  });

 
});



//$.ajax({
//     url: "/woniumall2/confirm.html",
//     type: "post",
//     success: function (responseResult) {
//       window.sessionStorage.setItem("goods", JSON.stringify(goods));
//       window.location.href = "/woniumall2/confirm.html?paynow=1";
//     },
//     error: function (responseResult) {
//       let resJson = responseResult.responseJSON;
//       alert("请登录后再操作");
//       window.location.href = resJson.data;
//     }
//   });


function cart_Nums() {
  //获得购物车的数量
 
  $.ajax({
    type: "POST",
    url: "/woniumall2/cart/getShoppCarts",
    success: function (responseResult) {
      if (responseResult.status == "SELECT_SUCCESS") {
        $("#cart_sums").text(responseResult.data);
      }
    },
    error: function (responseResult) {
      let resJson = responseResult.responseJSON;
      alert("请登录后再操作");
      window.location.href = resJson.data;
    }

  });
}

 //调用方法 查询是否是在线的
 var account = window.localStorage.getItem("account");
 var timer;
 function timer_login() {
   timer = window.setInterval(function islogin() {
     $.ajax({
       type: "POST",
       url: "/woniumall2/user/isLogin",
       success: function (responseResult) {
         console.log(responseResult);
         if (responseResult.status == "USERID_EXISTENCE") {
           //存在 
           //说明是正常登录
           console.log("登录ing");
         } else if (responseResult.status == "NO_LOGIN") {
           //不存在
           //说明可能被异地 或者 自己退出的
           if (account == null) {
             //说明是自己退出的
             console.log("正常退出");
           } else {
             //异地
             alert("你的帐号在别地登录了");
             window.localStorage.removeItem("account");
             window.location.href = "/woniumall2/index.html";
          
           }
           console.log("后台账号不在了");
         }
       },
     });
   }, 5000);
 }

 //判断有没有disabled这个属性
 function isDisabled(id_name) {

  let btn_class = $(id_name).attr("class").split(" ");

  for (let i = 0; i < btn_class.length; i++) {
    //console.log("1" + btn_class[i] + "1");
    if (btn_class[i] == 'disabled') {
      //返回true说明是disabled
      return true;
    }
  }
  //都不满足 说明没有该属性
  return false;
}

//生成指定的随机数
//生成从minNum到maxNum的随机数
function randomNum(minNum, maxNum) {
  switch (arguments.length) {
    case 1:
      return parseInt(Math.random() * minNum + 1, 10);
      break;
    case 2:
      return parseInt(Math.random() * (maxNum - minNum + 1) + minNum, 10);
      break;
    default:
      return 0;
      break;
  }
} 



